package o;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.nio.channels.ClosedSelectorException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

public final class fvi {
    private volatile boolean ʻ = false;
    private final ArrayList<InetSocketAddress> ˊ = new ArrayList();
    private final ArrayList<InetSocketAddress> ˋ;
    private final ArrayList<c> ˎ = new ArrayList();
    private final int ˏ;
    private int ॱ;
    private Selector ᐝ;

    final class c {
        final /* synthetic */ fvi ˊ;
        InetSocketAddress ˋ;
        SocketChannel ˎ;
        long ॱ;

        private c(fvi o_fvi) {
            this.ˊ = o_fvi;
            this.ˎ = null;
            this.ˋ = null;
            this.ॱ = 0;
        }

        void ˊ(InetSocketAddress inetSocketAddress, long j, Proxy proxy, ftz o_ftz, fum o_fum) throws IOException {
            this.ˋ = inetSocketAddress;
            this.ॱ = TimeUnit.NANOSECONDS.toMillis(System.nanoTime()) + j;
            o_fum.connectStart(o_ftz, inetSocketAddress, proxy);
            this.ˎ = SocketChannel.open();
            this.ˎ.configureBlocking(false);
            this.ˎ.connect(inetSocketAddress);
        }

        void ˏ() {
            if (this.ˎ != null) {
                try {
                    this.ˎ.close();
                } catch (Throwable e) {
                    this.ˊ.ˏ("Socket channel close error", e);
                } finally {
                    this.ˎ = null;
                }
            }
        }

        boolean ˎ() {
            return TimeUnit.NANOSECONDS.toMillis(System.nanoTime()) >= this.ॱ;
        }

        long ˋ() {
            long toMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
            if (toMillis >= this.ॱ) {
                return 0;
            }
            return this.ॱ - toMillis;
        }
    }

    public fvi(ArrayList<InetSocketAddress> arrayList, int i) {
        this.ˋ = new ArrayList(arrayList);
        this.ॱ = i;
        this.ˏ = arrayList.size();
    }

    public Socket ˊ(long j, Proxy proxy, ftz o_ftz, fum o_fum) {
        SocketChannel ˏ;
        Throwable e;
        try {
            this.ᐝ = Selector.open();
            try {
                ˏ = ˏ(j, proxy, o_ftz, o_fum);
                if (ˏ != null) {
                    try {
                        ˏ.configureBlocking(true);
                    } catch (IOException e2) {
                        try {
                            ˏ.close();
                            ˏ = null;
                        } catch (Throwable e3) {
                            ˏ("Socket channel close error", e3);
                            ˏ = null;
                        }
                    } catch (ClosedSelectorException e4) {
                        e = e4;
                        ˏ("Selector is already closed", e);
                    }
                }
            } catch (Throwable e32) {
                e = e32;
                ˏ = null;
            }
            ˋ();
            if (ˏ != null) {
                return ˏ.socket();
            }
            return null;
        } catch (IOException e5) {
            return null;
        }
    }

    public ArrayList<InetSocketAddress> ˏ() {
        return this.ˊ;
    }

    public void ˊ() {
        if (this.ᐝ != null) {
            try {
                this.ʻ = true;
                this.ᐝ.close();
            } catch (Throwable e) {
                ˏ("Selector close error", e);
            } finally {
                this.ᐝ = null;
            }
        }
    }

    private void ˏ(String str, Throwable th) {
        fwy.ˋ().ˏ(4, str, th);
    }

    private SocketChannel ˏ(long j, Proxy proxy, ftz o_ftz, fum o_fum) {
        long j2 = (long) this.ॱ;
        long j3 = j - ((long) this.ॱ);
        Object obj = 1;
        SocketChannel socketChannel = null;
        while (!this.ʻ) {
            Object obj2;
            if (this.ˋ.size() <= 0 || obj == null) {
                obj2 = obj;
            } else if (ˊ((InetSocketAddress) this.ˋ.remove(0), j, proxy, o_ftz, o_fum)) {
                j3 += (long) this.ॱ;
                obj2 = null;
            } else {
                continue;
            }
            if (this.ˎ.isEmpty() && this.ˋ.isEmpty()) {
                return null;
            }
            long ˋ = this.ˎ.size() > 0 ? ((c) this.ˎ.get(0)).ˋ() : j3;
            if (this.ˋ.size() > 0 && ˋ > j2) {
                ˋ = j2;
            }
            long nanoTime = System.nanoTime();
            try {
                this.ᐝ.select(ˋ);
                if (this.ʻ) {
                    return null;
                }
                ˋ = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - nanoTime);
                SocketChannel ˎ = ˎ();
                if (ˎ != null) {
                    return ˎ;
                }
                ˎ(ˋ);
                if (this.ˊ.size() == this.ˏ || ˋ >= j3) {
                    return ˎ;
                }
                Object obj3;
                nanoTime = j3 - ˋ;
                if (this.ˋ.size() <= 0) {
                    obj3 = obj2;
                    ˋ = j2;
                } else if (ˋ >= j2) {
                    ˋ = ((long) this.ॱ) - ((ˋ - j2) % ((long) this.ॱ));
                    obj3 = 1;
                } else {
                    ˋ = j2 - ˋ;
                    obj3 = obj2;
                }
                j3 = nanoTime;
                obj = obj3;
                j2 = ˋ;
                socketChannel = ˎ;
            } catch (IOException e) {
                return null;
            }
        }
        return socketChannel;
    }

    private boolean ˊ(InetSocketAddress inetSocketAddress, long j, Proxy proxy, ftz o_ftz, fum o_fum) {
        c cVar = new c();
        try {
            cVar.ˊ(inetSocketAddress, j, proxy, o_ftz, o_fum);
            cVar.ˎ.register(this.ᐝ, 8).attach(cVar);
            this.ˎ.add(cVar);
            return true;
        } catch (Throwable e) {
            ˏ("Failed to parepare socket channel for " + inetSocketAddress.toString(), e);
            this.ˊ.add(inetSocketAddress);
            cVar.ˏ();
            return false;
        }
    }

    private void ˊ(c cVar) {
        this.ˊ.add(cVar.ˋ);
        this.ˎ.remove(cVar);
        cVar.ˏ();
    }

    private void ˎ(long j) {
        while (this.ˎ.size() > 0) {
            c cVar = (c) this.ˎ.get(0);
            if (cVar.ˎ()) {
                ˊ(cVar);
            } else {
                return;
            }
        }
    }

    private SocketChannel ˎ() {
        SocketChannel socketChannel = null;
        Iterator it = this.ᐝ.selectedKeys().iterator();
        while (it.hasNext()) {
            SocketChannel socketChannel2;
            SelectionKey selectionKey = (SelectionKey) it.next();
            it.remove();
            if (selectionKey.isConnectable()) {
                c cVar = (c) selectionKey.attachment();
                try {
                    SocketChannel socketChannel3 = cVar.ˎ;
                    if (socketChannel3.finishConnect()) {
                        selectionKey.cancel();
                        try {
                            this.ˎ.remove(cVar);
                            return socketChannel3;
                        } catch (IOException e) {
                            socketChannel = socketChannel3;
                        }
                    } else {
                        selectionKey.cancel();
                        ˊ(cVar);
                        socketChannel2 = socketChannel;
                        socketChannel = socketChannel2;
                    }
                } catch (IOException e2) {
                    selectionKey.cancel();
                    ˊ(cVar);
                    socketChannel2 = socketChannel;
                    socketChannel = socketChannel2;
                }
            }
            socketChannel2 = socketChannel;
            socketChannel = socketChannel2;
        }
        return socketChannel;
    }

    private void ˋ() {
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((c) it.next()).ˏ();
        }
        this.ˎ.clear();
        if (this.ᐝ != null) {
            try {
                this.ᐝ.close();
            } catch (Throwable e) {
                ˏ("Selector close error", e);
            } finally {
                this.ᐝ = null;
            }
        }
    }
}
